package com.zenithsun.news.newsreview.dao.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.SQLQuery;
import org.springframework.stereotype.Repository;

import com.zenithsun.common.bean.ModelType;
import com.zenithsun.common.dao.NativeSQLDaoHibernate;
import com.zenithsun.news.newsreview.dao.NewsReviewDAO;
import com.zenithsun.news.newsreview.model.NewsReview;

@Repository("newsReviewDao")
public class NewsReviewDAOImpl extends NativeSQLDaoHibernate<NewsReview> implements NewsReviewDAO {

	/**
	 * countNewsReview的查询SQL
	 */
	private String SQL_countNewsReview="SELECT COUNT(N.ID) as totalCount FROM NEWSINFO N,REVIEW R";
	
	/**
	 * listNewsReview的查询SQL
	 */
	private String SQL_listNewsReview="SELECT n.title,n.correspondent,n.photographer,n.editor,n.type,n.cover_path,n.create_time,n.nabstract,(SELECT COUNT(ND.ID) FROM NEWSDETAILINFO ND WHERE ND.NEW_ID=N.ID) AS picNum,R.id,R.state,R.busId,R.mark,R.comments,R.bus_mark FROM NEWSINFO N,REVIEW R ";
	
	/**
	 * findNewsReview的查询SQL
	 */
	private String SQL_findNewsReview="SELECT N.TITLE,null as CORRESPONDENT,null as PHOTOGRAPHER,null as EDITOR,null as TYPE,null as COVER_PATH,null as CREATE_TIME,null as nabstract,0 AS picNum,R.* FROM NEWSINFO N,REVIEW R WHERE N.ID=R.BUSID AND R.ID=?";
	@Override
	protected Class<NewsReview> getEntityClass() {
		return NewsReview.class;
	}
	/**
	 * 获取条件,state等于-1则不适用状态作为条件
	 * @param searchCondition
	 * @return
	 */
	private String getCondition(String sql,NewsReview searchCondition){
		StringBuffer sb=new StringBuffer();
		sb.append(" WHERE N.ID=R.BUSID ").append(" AND N.ISDELETE=0").append(" AND R.BUS_MARK='").append(ModelType.News.toString()).append("'");
		if(searchCondition.getState()>-1){
			sb.append(" AND R.STATE= ").append(searchCondition.getState());
		}
		if(StringUtils.isNotBlank(searchCondition.getId())){
			sb.append(" AND N.id ='").append(searchCondition.getId()).append("'");
		}
		if(StringUtils.isNotBlank(searchCondition.getTitle())){
			sb.append(" AND N.TITLE like '%").append(searchCondition.getTitle()).append("%'");
		}
		if(sb.length()==0){
			return "";
		}else{
			sb.insert(0, sql);
		}
		if(searchCondition.getState()>-1){
			sb.append(" ORDER BY N.CREATE_TIME DESC,N.IS_TOP DESC");
		}else{
			sb.append(" ORDER BY N.PUBLISH_TIME DESC,N.IS_TOP DESC");
		}
		return sb.toString();
	}
	
	@Override
	public List<NewsReview> listNewsReview(int pageNo, int pageSize, NewsReview searchCondition) throws Exception {
		SQLQuery query=getSession().createSQLQuery(getCondition(SQL_listNewsReview,searchCondition));
		query.addEntity(getEntityClass());
		//设置分页条件
		query.setFirstResult((pageNo - 1) * pageSize).setMaxResults(pageSize);
		List list=query.list();
		return list;
	}

	@Override
	public int countNewsReview(NewsReview searchCondition) throws Exception {
		SQLQuery query=getSession().createSQLQuery(getCondition(SQL_countNewsReview,searchCondition));
		return getCountBySQL(query);
	}
	
	@Override
	public NewsReview findNewsReview(String revId) throws Exception {
		SQLQuery query=getSession().createSQLQuery(SQL_findNewsReview);
		query.addEntity(this.getEntityClass());
		query.setParameter(0, revId);
		List list=query.list();
		if(list!=null&&list.size()>0){
			return (NewsReview)list.get(0);
		}
		return null;
	}
	
}
